package demo5;

import javax.naming.Name;
import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2023-05-08
 * Time: 21:45
 */
class Student implements Comparable<Student> {
    public String name;
    public int age;
    public int score;

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    //没有实现比较器就使用默认compareTo
    @Override
    public int compareTo(Student o1) {
        return this.age - o1.age;
    }
}

//使用比较器
class AgeComparetor implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}

class NameComparetor implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}

class ScoreComparetor implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o2.score - o1.score;
    }
}

public class Testdemo1 {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhagnsan", 10, 19);
        students[1] = new Student("wangwu", 8, 78);
        students[2] = new Student("lisi", 15, 57);

        AgeComparetor ageComparetor = new AgeComparetor();

        NameComparetor nameComparetor = new NameComparetor();

        ScoreComparetor scoreComparetor = new ScoreComparetor();
        Arrays.sort(students,scoreComparetor);
        System.out.println(Arrays.toString(students));
    }

}
